package code.juc.stopThread;

/**
 * 情况说明:
 * 1. try...catch在while循环中.
 * 2. 无法被中断, 加了isInterrupted()判断, 也无用.
 *
 * 解析:
 * 1. 当sleep被interrupt()中断触发的时候, 就会清除掉中断标识; 导致isInterrupted()判断失效.
 */
public class CantInterrupt {

    public static void main(String[] args) throws InterruptedException {
        Runnable runnable = () -> {
            int num = 0;
            while (num < 10_000 && !Thread.currentThread().isInterrupted()) {
                if (num % 100 == 0) {
                    System.out.println(num + "is mod 100!");
                }

                num++;
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        Thread thread = new Thread(runnable);
        thread.start();
        Thread.sleep(5_000);
        thread.interrupt();
    }
}
